package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.MessageMapper;
import com.liuzu.esys.pojo.Message;
import com.liuzu.esys.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("messageService")
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Override
    public LayuiPageVo<Message> messagePageData(Map<String, Object> params) {
        //mybatis 的分页插件
        Integer page = Integer.parseInt(params.get("page") + "");

        Integer limit = Integer.parseInt(params.get("limit") + "");

        PageHelper.startPage(page, limit);

        List<Message> messages = messageMapper.selectByParams(params);

        //mybatis 的分页数据结构
        PageInfo<Message> userPageInfo = new PageInfo<>(messages);

        //把mybatis 的分页数据封装到 layui 的分页数据结构中
        LayuiPageVo<Message> messageLayuiPageVo = new LayuiPageVo<>();
        messageLayuiPageVo.setCode(0); //layui 默认0 是正确状态
        messageLayuiPageVo.setMsg("分页列表数据");  //提示信息
        messageLayuiPageVo.setCount(userPageInfo.getTotal());//总记录数
        messageLayuiPageVo.setData(userPageInfo.getList());

        return messageLayuiPageVo;
    }

    @Override
    public int addMessage(Message message) {
        int i = messageMapper.insertSelective(message);
        return i;
    }

    @Override
    public int updateMessage(Message message) {
        int i = messageMapper.updateMessage(message);
        return i;
    }

    @Override
    public int deleteMessage(Integer id) {

        return messageMapper.deleteByPrimarKey(id);
    }

    @Override
    public int deleteMessageByIds(String ids) {
        String[] arrId = ids.split(",");
        return messageMapper.deleteMessageByIds(arrId);

    }
}
